package com.tomatozq163;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;

public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;

    public int getCanvasWidth() {
        return canvasWidth;
    }

    public int getCanvasHeight() {
        return canvasHeight;
    }

    public AlgoFrame(String title, int canvasWidth, int canvasHeight){
        super(title);

        this.canvasHeight = canvasHeight;
        this.canvasWidth = canvasWidth;

        AlgoCanvas canvas = new AlgoCanvas();

//        canvas.setPreferredSize(new Dimension(canvasWidth,canvasHeight));
//        this.setSize(canvasWidth,canvasHeight);

        //pane 右下角有padding
        setContentPane(canvas);

        //这个边距信息实际上是在pack()时添加的，我们先强制让系统也不能resize我们的窗口，再pack，就可以解决这个问题了
        setResizable(false);
        //调整布局
        pack();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);
    }

    public AlgoFrame(String title){
        this(title,1024,768);
    }

    private Circle[] circles;

    public void render(Circle[] circles){
        this.circles = circles;
        repaint();
    }

    private class AlgoCanvas extends JPanel{
        public AlgoCanvas() {
            super(true);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

//            g.drawOval(50,50,300,300);
            Graphics2D g2d = (Graphics2D)g;

            //抗锯齿
            RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(hints);

            g2d.setColor(Color.PINK);
            g2d.fillRect(0,0,canvasWidth,canvasHeight);

            AlgoVisHelper.setStrokeWidth(g2d,1);
            AlgoVisHelper.setColor(g2d,Color.RED);

            if (circles!=null) {
                for (Circle circle : circles) {
                    if (circle.isFilled){
                        AlgoVisHelper.fillCircle(g2d,circle.x,circle.y,circle.getR());
                    }
                    else {
                        AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR());
                    }
                }
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(canvasWidth,canvasHeight);
        }
    }
}
